﻿Public Class frmLandingPage

    ' Service used for basic CRUD operations
    Private contactService As ContactService = New ContactServiceImpl()

    Private Sub frmLandingPage_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        loadContacts()
        frmEditContact.contactService = contactService
        
    End Sub

    Public Sub loadContacts()

        dgContacts.AutoGenerateColumns = False
        dgContacts.AutoSize = True
        dgContacts.DataSource = contactService.getContactList

    End Sub

    
    Private Sub dgContacts_CellContentClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgContacts.CellContentClick

        If e.RowIndex = -1 Then
            ' User Clicked on header
            Return
        End If

        hideMessageText()

        Dim id As Long = dgContacts(0, e.RowIndex).Value
        Dim action As String = dgContacts(e.ColumnIndex, e.RowIndex).Value
        If (action = "Edit") Then
            frmEditContact.contactId = id
            frmEditContact.ShowDialog()
        ElseIf (action = "Delete") Then
            Dim isSuccess As Boolean = contactService.deleteContact(id)
            If isSuccess Then
                displayMessageOfType(MessageType.SUCCESS, "Contact Deleted.")
                loadContacts()
            Else
                displayMessageOfType(MessageType.FAIL, "Unable to delete contact at this time.")
            End If
        End If

    End Sub


    Public Sub displayMessageOfType(type As MessageType, msg As String)

        If type = MessageType.SUCCESS Then
            lblMessageText.BackColor = Color.LightGreen
            lblMessageText.ForeColor = Color.DarkGreen
        Else
            lblMessageText.BackColor = Color.DarkRed
            lblMessageText.BackColor = Color.Magenta
        End If

        lblMessageText.Text = msg
        lblMessageText.Visible = True

    End Sub

    Public Sub hideMessageText() Handles lblMessageText.Click
        lblMessageText.Visible = False
    End Sub

    Private Sub lnkAddNewContact_LinkClicked(sender As System.Object, e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles lnkAddNewContact.LinkClicked

        hideMessageText()
        frmEditContact.contactId = -1
        frmEditContact.ShowDialog()

    End Sub

    Public Enum MessageType
        FAIL
        WARN
        SUCCESS
    End Enum

End Class
